<!-- 线路（架空线路、电力电缆） -->
<template>
  <el-dialog
    @close="close"
    :visible="show"
    append-to-body
    :close-on-click-modal="false"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(23,69,125,0.9)"
    width="500px"
  >
    <el-form :model="form" :rules="rules" label-width="120px" ref="form">
      <el-form-item label="电压等级" prop="dydj">
        <el-select v-model="form.dydj" disabled>
          <el-option
            v-for="item in commonOpts.dydjOpt"
            :key="item.value"
            :value="item.value"
            :label="item.text"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="投运日期" prop="tyrq">
        <el-date-picker
          v-model="form.tyrq"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="所属大馈线" prop="ssdkx">
        <el-select v-model="form.ssdkx" @change="changeSsdkx" allow-create filterable :multiple-limit="limitValue">
          <el-option
            v-for="item in commonOpts.ssdkxOpt"
            :key="item.oid"
            :value="item.oid"
            :label="item.sbmc"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="线路长度" prop="dxcd">
        <el-input v-model="form.dxcd">
          <template slot="append">km</template>
        </el-input>
      </el-form-item>
      <el-form-item label="型号" prop="sbxh">
        <el-select v-model="form.sbxh">
          <el-option
            v-for="item in commonOpts.xlxhOpt"
            :key="item.sbxh"
            :value="item.sbxh"
            :label="item.sbxh"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起点设备">
        <el-input v-model="startSbmc" disabled></el-input>
      </el-form-item>
      <el-form-item label="终点设备">
        <el-input v-model="finishSbmc" disabled></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button type="primary" @click="save">设备保存</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'Xl',
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    getMap: Function,
    type: String,
    params: Object
  },
  data() {
    return {
      limitValue: 1,
      loading: false,
      commonOpts: {
        dydjOpt: [{ value: 22, text: '10kV' }],
        xlxhOpt: [],
        ssdkxOpt: []
      },
      form: {
        dydj: 22,
        ssdkx: '新增大馈线',
        dxcd: '',
        tyrq: '2022-01-01',
        sbxh: '',
        dkxOid: '',
        dkxSbmx: '',
        dkxVersionid: ''
      },
      startSbmc: '',
      finishSbmc: '',
      rules: {
        dydj: [{ required: true, message: '请选择电压等级', trigger: 'change' }],
        ssdkx: [{ required: true, message: '请选择所属大馈线', trigger: 'change' }],
        dxcd: [{ required: true, message: '请输入线路长度', trigger: 'change' }],
        sbxh: [{ required: true, message: '请选择设备型号', trigger: 'change' }],
        tyrq: [{ required: true, message: '请选择投运日期', trigger: 'change' }]
      }
    };
  },
  watch: {
    show(newVal) {
      if (newVal) {
        this.initParams();
      }
    }
  },
  methods: {
    initParams() {
      this.initQyDkxOpt();
      this.initXlxhcsOpt();
      let data = this.params.data;
      this.$nextTick(() => {
        this.form.dydj = data.otherfields.dydj;
        this.form.dxcd = Number(Number(data.otherfields.dxcd).toFixed(2));
        this.form.sbxh = data.sbzlx * 1 === 10100000 ? this.params.defaultOptions.jkxh : this.params.defaultOptions.dlxh;
        this.startSbmc = data.startEquip && data.startEquip.sbmc;
        this.finishSbmc = data.endEquip && data.endEquip.sbmc;
        if (this.type === 'edit') {
          this.form.ssdkx = data.otherfields.ssdkx;
        }
      });
    },
    initQyDkxOpt() {
      let params = {
        schema: this.params.defaultOptions.schema,
        ssghrw: this.params.defaultOptions.ghrw,
        qyid: this.params.defaultOptions.ssqy,
        fxlx: 2,
        sbzlx: 10000100 // 大馈线
      };
      this.getMap()
        .api.PwghGhsjJcfw.Sbtz.post_Sbtz_querySbBySbqygl({ params: params })
        .then((res) => {
          if (res.resp_code === 200 && res.datas) {
            this.commonOpts.ssdkxOpt = res.datas[10000100];
            this.changeSsdkx(this.form.ssdkx);
          }
        });
    },
    initXlxhcsOpt() {
      let params = {
        schema: this.params.defaultOptions.schema,
        ghrw: this.params.defaultOptions.ghrw,
        sbzlx: this.params.data.sbzlx,
        pageNo: 1,
        pageSize: 1000
      };
      this.getMap()
        .api.PwghGhsjJcfw.xlxhcsController.post_xlxhcsController_queryXlxhcslb({ params: params })
        .then((res) => {
          this.commonOpts.xlxhOpt = res.datas.xlxhcsList;
        });
    },
    changeSsdkx(val) {
      let obj = this.commonOpts.ssdkxOpt.find(function(item) {
        return item.oid === val;
      });
      if (obj) {
        this.form.dkxOid = obj.oid;
        this.form.dkxSbmx = obj.sbmc;
        this.form.dkxVersionid = obj.versionid;
      } else {
        this.form.dkxOid = null;
        this.form.dkxSbmx = val;
        this.form.dkxVersionid = null;
      }
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          let params = this.params.data;
          params.otherfields = Object.assign({}, params.otherfields, this.form);
          delete params.otherfields['ssdkx'];
          if (this.type === 'add') {
            this.getMap()
              .api.PwghGhsjTxfw.sbjh.post_sbjh_addEquips({
                params: { verRule: this.params.defaultOptions.verRule },
                data: params
              })
              .then((res) => {
                this.loading = false;
                if (res.resp_code === 200) {
                  this.$message({
                    type: 'success',
                    message: '保存成功'
                  });
                  this.close();
                }
              })
              .catch(() => {
                this.loading = false;
              });
          } else if (this.type === 'edit') {
            this.getMap()
              .api.PwghGhsjTxfw.sbjh.post_sbjh_modifyEquips({
                params: { verRule: this.params.defaultOptions.verRule },
                data: params
              })
              .then((res) => {
                this.loading = false;
                if (res.resp_code === 200) {
                  this.$message({
                    type: 'success',
                    message: '保存成功'
                  });
                  this.close();
                }
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      });
    },
    close() {
      this.$refs.form.resetFields();
      this.$emit('update:show', false);
      this.$emit('refresh');
    }
  }
};
</script>

<style lang="less" scoped></style>
